package silentDonkey.service.impl;

import silentDonkey.dao.UserDao;
import silentDonkey.dao.impl.UserDaoImpl;
import silentDonkey.domain.Admin;
import silentDonkey.domain.PageBean;
import silentDonkey.domain.User;
import silentDonkey.service.UserService;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    // 声明DAO实现类对象
    private UserDao dao = new UserDaoImpl();

    @Override
    public List<User> findAll() {
        // 调用dao进行查询
        return dao.findAll();
    }

    @Override
    public Admin login(Admin admin) {
        return dao.findAdminByNameAndPasswd(admin.getUsername(), admin.getPassword());
    }

    @Override
    public void addUser(User user) {
        dao.add(user);
    }

    @Override
    public void deleteUser(String id) {
        dao.delete(Integer.parseInt(id));
    }

    @Override
    public User findUserById(String id) {
        return dao.findById(Integer.parseInt(id));

    }

    @Override
    public void updateUser(User user) {
        dao.update(user);
    }

    @Override
    public void deleteSelectedUser(String... ids) {
        // 遍历之前应该也先做数据校验->避免空指针异常
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                dao.delete(Integer.parseInt(id));
            }
        }


    }

    @Override
    public PageBean<User> findUserByPage(String _currentPage, String _rows, Map<String, String[]> condition) {
//        int currentPage = Integer.parseInt(_currentPage);
//        int rows = Integer.parseInt(_rows);
//
//        // 1.创建一个空的PageBean对象
//        PageBean<User> pb = new PageBean<>();
//
//        // 3.调用dao查询总记录数
//        int totalCount = dao.findTotalCount(condition);
//        pb.setTotalCount(totalCount);
//        // 5.计算总页码
//        int totalPage = (totalCount % rows) == 0 ? totalCount % rows : (totalCount % rows) + 1;
//        pb.setTotalPage(totalPage);
//        // 判断currentPage是否合理
//        if (currentPage < 1) {
//            currentPage = 1;
//        }
//        if (currentPage > totalPage) {
//            currentPage = totalPage;
//        }
//        // 2.设置参数
//        pb.setCurrentPage(currentPage);
//        pb.setRows(rows);
//        // 4.调用dao查询List集合
//        int start = (currentPage - 1) * rows;
//        List<User> list = dao.findByPage(start, rows, condition);
//        pb.setList(list);
//
//        return pb;
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        if (currentPage <= 0) {
            currentPage = 1;
        }


        //1.创建空的PageBean对象
        PageBean<User> pb = new PageBean<User>();
        //2.设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3.调用dao查询总记录数
        int totalCount = dao.findTotalCount(condition);
        pb.setTotalCount(totalCount);


        //5.计算总页码
        int totalPage = (totalCount % rows) == 0 ? totalCount / rows : (totalCount / rows) + 1;
        pb.setTotalPage(totalPage);
        if (currentPage > totalPage) {
            currentPage = totalPage;
            pb.setCurrentPage(totalPage);
        }
        System.out.println("当前总页数：" + pb);
        //4.调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<User> list = dao.findByPage(start, rows, condition);
        pb.setList(list);
        return pb;

    }
}
